﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Numbers
{
    /// <summary>
    /// Simple value tracker - notifies folks when something happens.
    /// </summary>
    public class ValueTracker : INotifyPropertyChanged
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; OnPropertyChanged("Name"); }
        }

        private double _value;
        public double Value {
            get { return _value; }
            private set { _value = value; OnPropertyChanged("Value"); }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// We need to track the changes that happen to a value!
        /// </summary>
        /// <param name="v"></param>
        public ValueTracker(Value v)
        {
            Value = v.Val;
            v.PropertyChanged += new PropertyChangedEventHandler(v_PropertyChanged);
        }

        /// <summary>
        /// The property we are tracking has changed. Pick up the change
        /// and make sure that we propagate it to anyone of us that is listening.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void v_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            Value o = sender as Value;
            Value = o.Val;
            OnPropertyChanged("Value");
        }

        /// <summary>
        /// Notify clients of a property changing!
        /// </summary>
        /// <param name="propName"></param>
        private void OnPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }
    }
}
